<template>
  <div class="api-demo">
    <h2>Axios 封装使用示例</h2>
    
    <div class="demo-section">
      <h3>用户登录</h3>
      <el-form @submit.prevent="handleLogin">
        <el-form-item label="用户名">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="demo-section">
      <h3>获取用户信息</h3>
      <el-button @click="fetchUserInfo">获取用户信息</el-button>
      <div v-if="userInfo" class="user-info">
        <p>用户名: {{ userInfo.username }}</p>
        <p>邮箱: {{ userInfo.email }}</p>
      </div>
    </div>

    <div class="demo-section">
      <h3>文件上传</h3>
      <el-upload
        :before-upload="handleUpload"
        :show-file-list="false"
        accept="image/*"
      >
        <el-button type="success">上传头像</el-button>
      </el-upload>
    </div>

    <div class="demo-section">
      <h3>请求示例（模拟错误）</h3>
      <el-button @click="testErrorRequest">测试错误请求</el-button>
      <el-button @click="testNoLoadingRequest">无Loading请求</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { userApi, request } from '@/utils/api'
import type { User, LoginParams } from '@/utils/api'

// 登录表单
const loginForm = reactive<LoginParams>({
  username: '',
  password: ''
})

// 用户信息
const userInfo = ref<User | null>(null)

// 处理登录
const handleLogin = async () => {
  try {
    if (!loginForm.username || !loginForm.password) {
      ElMessage.warning('请填写用户名和密码')
      return
    }

    const response = await userApi.login(loginForm)
    
    // 保存token
    localStorage.setItem('token', response.data.token)
    userInfo.value = response.data.user
    
    ElMessage.success('登录成功')
  } catch (error) {
    console.error('登录失败:', error)
    // 错误信息已经在axios拦截器中处理了
  }
}

// 获取用户信息
const fetchUserInfo = async () => {
  try {
    const response = await userApi.getUserInfo()
    userInfo.value = response.data
    ElMessage.success('获取用户信息成功')
  } catch (error) {
    console.error('获取用户信息失败:', error)
  }
}

// 处理文件上传
const handleUpload = async (file: File) => {
  try {
    const response = await userApi.uploadAvatar(file)
    ElMessage.success(`头像上传成功: ${response.data.url}`)
  } catch (error) {
    console.error('上传失败:', error)
  }
  return false // 阻止默认上传行为
}

// 测试错误请求
const testErrorRequest = async () => {
  try {
    await request.get('/api/nonexistent-endpoint')
  } catch (error) {
    console.error('预期中的错误:', error)
  }
}

// 测试无Loading的请求
const testNoLoadingRequest = async () => {
  try {
    await request.get('/api/test', { 
      showLoading: false // 不显示loading
    })
  } catch (error) {
    console.error('无loading请求错误:', error)
  }
}
</script>

<style scoped>
.api-demo {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
}

.demo-section {
  margin-bottom: 30px;
  padding: 20px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
}

.demo-section h3 {
  margin-top: 0;
  color: #409eff;
}

.user-info {
  margin-top: 15px;
  padding: 10px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.user-info p {
  margin: 5px 0;
}
</style>